﻿using Newtonsoft.Json;
using SaleManagement.Models;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SaleManagement.Controllers
{
    public class CategoriesController
    {
        private List<Categories_Model> ListCategories;
        private Status_Model status;
        public string mess = "";
        public CategoriesController()
        {
            ListCategories = new List<Categories_Model>();
            status = new Status_Model();
        }
        #region[getdata]
        public List<Categories_Model> getdata(int industry_id)
        {
            List<Categories_Model> md = new List<Categories_Model>();
            string urlAddress = "http://192.168.0.106/sale-server/public/" + industry_id + "/categories";
            using (WebClient client = new WebClient())
		    {
                String value= client.DownloadString(urlAddress);//lay json tu web ve
                ListCategories = JsonConvert.DeserializeObject<List<Categories_Model>>(value);
            }
            return ListCategories;
        }
        #endregion
        public bool insertcategory(int industry_id, string name, string description)
        {
            bool kt = false;
            string urlAddress = "http://192.168.0.106/sale-server/public/" + industry_id + "/category/insert";
            using(WebClient client = new WebClient()){
                NameValueCollection postdata = new NameValueCollection(){
                    {"name",name},
                    {"description",description}
                };
                String pagesource = Encoding.UTF8.GetString(client.UploadValues(urlAddress, "POST", postdata));
                bool status = pagesource.Contains("status");
                if (status == true)
                {
                    Status_Model mdStatus = JsonConvert.DeserializeObject<Status_Model>(pagesource);
                    mess = mdStatus.Status;
                    kt = true;
                   
                }
                else
                {
                    Error_Model mdError = JsonConvert.DeserializeObject<Error_Model>(pagesource);
                    mess = mdError.Error_msg;
                    kt = false;
                }
            }
            return kt;
        }//End 
        public bool deletecategory(string category_id)
        {
            bool kt = false;
            //{shop_id}/warehouse/delete
            string urlAddress = "http://192.168.0.106/sale-server/public/" + category_id + "/category/delete";
            using (WebClient client = new WebClient())
            {
                NameValueCollection postdata = new NameValueCollection(){
                    {"id",category_id}
                };
                String pagesource = Encoding.UTF8.GetString(client.UploadValues(urlAddress, "POST", postdata));
                bool status = pagesource.Contains("status");
                if (status == true)
                {
                    Status_Model mdStatus = JsonConvert.DeserializeObject<Status_Model>(pagesource);
                    mess = mdStatus.Status;
                    kt = true;
                }
                else
                {
                    Error_Model mdError = JsonConvert.DeserializeObject<Error_Model>(pagesource);
                    mess = mdError.Error_msg;
                    kt = false;
                }
            }
            return kt;
        }//End

        public bool Editcategory(string category_id, string name, string description)
        {
            bool kt = false;
            //{shop_id}/warehouse/delete
            string urlAddress = "http://192.168.0.106/sale-server/public/" + category_id + "/category/edit";
            using (WebClient client = new WebClient())
            {
                NameValueCollection postdata = new NameValueCollection(){
                    {"name",name},
                    {"description",description}
                };
                String pagesource = Encoding.UTF8.GetString(client.UploadValues(urlAddress, "POST", postdata));
                bool status = pagesource.Contains("status");
                if (status == true)
                {
                    Status_Model mdStatus = JsonConvert.DeserializeObject<Status_Model>(pagesource);
                    mess = mdStatus.Status;
                    kt = true;
                }
                else
                {
                    Error_Model mdError = JsonConvert.DeserializeObject<Error_Model>(pagesource);
                    mess = mdError.Error_msg;
                    kt = false;
                }
            }
            return kt;
        }//End

    }
}
